package com.king.run.activity.sport;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.king.run.R;
import com.king.run.activity.sport.adapter.MyFragmentPagerAdapter;
import com.king.run.base.BaseFragment;

import org.xutils.common.util.DensityUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：shuizi_wade on 2017/8/18 16:23
 * 邮箱：674618016@qq.com
 */
@ContentView(R.layout.fragment_sport)
public class SportFragment extends BaseFragment {
    @ViewInject(R.id.scrollview)
    HorizontalScrollView scrollView;
    @ViewInject(R.id.scrollview_bottom)
    HorizontalScrollView scrollview_bottom;
    @ViewInject(R.id.pager)
    ViewPager viewPager;
    //    @ViewInject(R.id.radio_group)
//    RadioGroup radio_group;
    @ViewInject(R.id.iv_frag1)
    ImageView iv_frag1;
    @ViewInject(R.id.iv_frag2)
    ImageView iv_frag2;
    @ViewInject(R.id.iv_frag3)
    ImageView iv_frag3;
    @ViewInject(R.id.iv_frag4)
    ImageView iv_frag4;
    private List<Fragment> mFragments = new ArrayList<>();
    private ExerciseFragment exerciseFragment;
    private RideFragment rideFragment;
    private WalkFragment walkFragment;
    private RunFragment runFragment;
    private Display d;
    @ViewInject(R.id.iv_first)
    ImageView iv_first;
    @ViewInject(R.id.iv_second)
    ImageView iv_second;
    @ViewInject(R.id.iv_three)
    ImageView iv_three;
    @ViewInject(R.id.iv_four)
    ImageView iv_four;
    @ViewInject(R.id.tv_first)
    TextView tv_first;
    @ViewInject(R.id.tv_second)
    TextView tv_second;
    @ViewInject(R.id.tv_three)
    TextView tv_three;
    @ViewInject(R.id.tv_four)
    TextView tv_four;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData(view);
//        initViews(view);
    }

    private void initData(View view) {
        //幕
        d = getActivity().getWindowManager().getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        d.getMetrics(dm);
        walkFragment = new WalkFragment();
        runFragment = new RunFragment();
        exerciseFragment = new ExerciseFragment();
        rideFragment = new RideFragment();
        mFragments.add(walkFragment);
        mFragments.add(runFragment);
        mFragments.add(exerciseFragment);
        mFragments.add(rideFragment);
        MyFragmentPagerAdapter tabPageAdapter = new MyFragmentPagerAdapter(
                getChildFragmentManager(), mFragments);
        viewPager.setAdapter(tabPageAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectPage(position);
                initFourScrollPic(position);
                initCirclePoint(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        selectPage(0);
        initFourScrollPic(0);
        initCirclePoint(0);
        view.findViewById(R.id.ly_first).setOnClickListener(listener);
        view.findViewById(R.id.ly_second).setOnClickListener(listener);
        view.findViewById(R.id.ly_three).setOnClickListener(listener);
        view.findViewById(R.id.ly_four).setOnClickListener(listener);
        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });
    }

    /**
     * 初始化界面四个滑动图片
     */
    private void initFourScrollPic(int position) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) scrollView.getLayoutParams();
        params.setMargins(d.getWidth() / 2 - (DensityUtil.dip2px(60) * position) - DensityUtil.dip2px(15),
                0, 0, DensityUtil.dip2px(140));
        scrollView.setLayoutParams(params);
    }

    /**
     * 初始化界面四个滑动圆点
     */
    private void initCirclePoint(int position) {
        RelativeLayout.LayoutParams paramsBottom = (RelativeLayout.LayoutParams) scrollview_bottom.getLayoutParams();
        paramsBottom.setMargins(d.getWidth() / 2 - ((DensityUtil.dip2px(10) + 10) * position) - DensityUtil.dip2px(5)
                , 0, 0, DensityUtil.dip2px(120));
        scrollview_bottom.setLayoutParams(paramsBottom);
        LinearLayout.LayoutParams layoutParams1 = (LinearLayout.LayoutParams) iv_frag1.getLayoutParams();
        LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) iv_frag2.getLayoutParams();
        LinearLayout.LayoutParams layoutParams3 = (LinearLayout.LayoutParams) iv_frag3.getLayoutParams();
        LinearLayout.LayoutParams layoutParams4 = (LinearLayout.LayoutParams) iv_frag4.getLayoutParams();
        switch (position) {
            case 0:
                layoutParams1.width = 20;
                layoutParams1.height = 10;
                iv_frag1.setLayoutParams(layoutParams1);
                layoutParams2.width = 10;
                layoutParams2.height = 10;
                iv_frag2.setLayoutParams(layoutParams2);
                layoutParams3.width = 10;
                layoutParams3.height = 10;
                iv_frag3.setLayoutParams(layoutParams3);
                layoutParams4.width = 10;
                layoutParams4.height = 10;
                iv_frag4.setLayoutParams(layoutParams4);
                iv_frag1.setBackgroundResource(R.drawable.iv_sport_fragment_black);
                iv_frag2.setBackgroundResource(R.drawable.iv_sport_fragment_gray);
                iv_frag3.setBackgroundResource(R.drawable.iv_sport_fragment_gray);
                iv_frag4.setBackgroundResource(R.drawable.iv_sport_fragment_gray);
                break;
            case 1:
                layoutParams1.width = 10;
                layoutParams1.height = 10;
                iv_frag1.setLayoutParams(layoutParams1);
                layoutParams2.width = 20;
                layoutParams2.height = 10;
                iv_frag2.setLayoutParams(layoutParams2);
                layoutParams3.width = 10;
                layoutParams3.height = 10;
                iv_frag3.setLayoutParams(layoutParams3);
                layoutParams4.width = 10;
                layoutParams4.height = 10;
                iv_frag4.setLayoutParams(layoutParams4);
                iv_frag1.setBackgroundResource(R.drawable.iv_sport_fragment_gray);
                iv_frag2.setBackgroundResource(R.drawable.iv_sport_fragment_black);
                iv_frag3.setBackgroundResource(R.drawable.iv_sport_fragment_gray);
                iv_frag4.setBackgroundResource(R.drawable.iv_sport_fragment_gray);
                break;
            case 2:
                layoutParams1.width = 10;
                layoutParams1.height = 10;
                iv_frag1.setLayoutParams(layoutParams1);
                layoutParams2.width = 10;
                layoutParams2.height = 10;
                iv_frag2.setLayoutParams(layoutParams2);
                layoutParams3.width = 20;
                layoutParams3.height = 10;
                iv_frag3.setLayoutParams(layoutParams3);
                layoutParams4.width = 10;
                layoutParams4.height = 10;
                iv_frag4.setLayoutParams(layoutParams4);
                iv_frag1.setBackgroundResource(R.drawable.iv_sport_fragment_gray);
                iv_frag2.setBackgroundResource(R.drawable.iv_sport_fragment_gray);
                iv_frag3.setBackgroundResource(R.drawable.iv_sport_fragment_black);
                iv_frag4.setBackgroundResource(R.drawable.iv_sport_fragment_gray);
                break;
            case 3:
                layoutParams1.width = 10;
                layoutParams1.height = 10;
                iv_frag1.setLayoutParams(layoutParams1);
                layoutParams2.width = 10;
                layoutParams2.height = 10;
                iv_frag2.setLayoutParams(layoutParams2);
                layoutParams3.width = 10;
                layoutParams3.height = 10;
                iv_frag3.setLayoutParams(layoutParams3);
                layoutParams4.width = 20;
                layoutParams4.height = 10;
                iv_frag4.setLayoutParams(layoutParams4);
                iv_frag1.setBackgroundResource(R.drawable.iv_sport_fragment_gray);
                iv_frag2.setBackgroundResource(R.drawable.iv_sport_fragment_gray);
                iv_frag3.setBackgroundResource(R.drawable.iv_sport_fragment_gray);
                iv_frag4.setBackgroundResource(R.drawable.iv_sport_fragment_black);
                break;
        }
    }

    /**
     * 选择某页
     *
     * @param position 页面的位置
     */
    private void selectPage(int position) {
        // 将所有的tab的icon变成灰色的
        for (int i = 0; i < 4; i++) {
            iv_first.setBackgroundResource(R.mipmap.first_false);
            iv_second.setBackgroundResource(R.mipmap.sceond_false);
            iv_three.setBackgroundResource(R.mipmap.three_false);
            iv_four.setBackgroundResource(R.mipmap.four_false);
            tv_first.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_color_gray));
            tv_second.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_color_gray));
            tv_three.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_color_gray));
            tv_four.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_color_gray));
        }
        // 切换页面
        viewPager.setCurrentItem(position, false);
        // 改变图标
        switch (position) {
            case 0:
                iv_first.setBackgroundResource(R.mipmap.first_true);
                tv_first.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_color_3));
                break;
            case 1:
                iv_second.setBackgroundResource(R.mipmap.second_true);
                tv_second.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_color_3));
                break;
            case 2:
                iv_three.setBackgroundResource(R.mipmap.three_true);
                tv_three.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_color_3));
                break;
            case 3:
                iv_four.setBackgroundResource(R.mipmap.four_true);
                tv_four.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_color_3));
                break;
        }
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.ly_first:
                    selectPage(0);
                    initFourScrollPic(0);
                    break;
                case R.id.ly_second:
                    selectPage(1);
                    initFourScrollPic(1);
                    break;
                case R.id.ly_three:
                    selectPage(2);
                    initFourScrollPic(2);
                    break;
                case R.id.ly_four:
                    selectPage(3);
                    initFourScrollPic(3);
                    break;
            }
        }
    };
}
